package java8Test.menu;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class App {
	private static List<Dish> menu = Arrays.asList(
					new Dish("beef", false, 800, Dish.Type.MEAT),
					new Dish("pork", false, 800, Dish.Type.MEAT),
					new Dish("chicken", false, 400, Dish.Type.MEAT),
					new Dish("french", true, 530, Dish.Type.OTHER),
					new Dish("rice", true, 350, Dish.Type.OTHER),
					new Dish("season", true, 120, Dish.Type.OTHER),
					new Dish("pizza", true, 550, Dish.Type.OTHER),
					new Dish("prawns", false, 300, Dish.Type.FISH),
					new Dish("salmon", false, 450, Dish.Type.FISH)
					);
					
	
	public static void main(String[] args) {
		Map<Boolean, List<Dish>> partitionedMenu1 = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian));
		System.out.println(partitionedMenu1);
		
		Map<Boolean, Map<Dish.Type, List<Dish>>> partitionedMenu2 = menu.stream().collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.groupingBy(Dish::getType)));
		System.out.println(partitionedMenu2);
		
		Map<Boolean, Dish> partitionedMenu3 = menu.stream()
						.collect(Collectors.partitioningBy(Dish::isVegetarian, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(Dish::getCalories)), Optional::get)));
		System.out.println(partitionedMenu3);

		long count = menu.stream().filter(e -> e.getCalories() > 500).count();
		System.out.println(count);

		// 惰性刻画出stream 并未执行
		menu.stream().filter(e -> {
			System.out.println(e.getName());
			return false;
		});
		// map 使用 Function
		menu.stream().map(Dish::getName).collect(Collectors.toList()).forEach(System.out::println);
		System.out.println(menu.stream().mapToInt(Dish::getCalories).collect((Supplier<ArrayList>) ArrayList::new, List::add, List::addAll));
		System.out.println(menu.stream().mapToInt(Dish::getCalories).boxed().collect(Collectors.toList()));

//		menu.stream().mapToInt(Dish::getCalories).co

	}
}
